﻿using Autofac;
using Common;
using DBContext;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using ZoneLine.Common;
using ZoneLine.Models;
using ZoneLine.Stations;

namespace ZoneLine.Views
{
    /// <summary>
    /// TestView.xaml 的交互逻辑
    /// </summary>
    public partial class TestView : Window
    {
        public TestView()
        {
            InitializeComponent();
        }

        private void BtnEnable(object sender, RoutedEventArgs e)
        {
            if (LX.IsChecked == true)
            {
                Cache.Model = ModelEnum.LX;
            }
            else if (SQ.IsChecked == true)
            {
                Cache.Model = ModelEnum.SQ;
            }
            else if (BQKH.IsChecked == true)
            {
                Cache.Model = ModelEnum.BQKH;
            }
            else if (BQLH.IsChecked == true)
            {
                Cache.Model = ModelEnum.BQLH;
            }
            else if (WL.IsChecked == true)
            {
                Cache.Model = ModelEnum.WL;
            }
            else
            {
                MessageBox.Show(this, "必须选择型号后才能进入工作流程");
                return;
            }

            var workDirection = 0;
            if (EnableL.IsChecked.HasValue && EnableL.IsChecked.Value)
            {
                workDirection = 1;
                Cache.WorkDirection = Common.WorkDirectionEnum.Left;
            }
            if (EnableR.IsChecked.HasValue && EnableR.IsChecked.Value)
            {
                workDirection = 2;
                Cache.WorkDirection = Common.WorkDirectionEnum.Right;
            }
            var dal = ContainerServer.Container.Resolve<IModelDAL>();
            var saveRlt = dal.UpdateModel<StationEnableConfig>(p => true, s =>
                 new StationEnableConfig()
                 {
                     OP006StationEnable = StationOP006.IsChecked == true ? 1 : 0,
                     OP010StationEnable = StationOP010.IsChecked == true ? 1 : 0,
                     OP020StationEnable = StationOP020.IsChecked == true ? 1 : 0,
                     OP050LStationEnable = StationOP050L.IsChecked == true ? 1 : 0,
                     OP050RStationEnable = StationOP050R.IsChecked == true ? 1 : 0,
                     WorkDirection = workDirection,
                 }
            );
            short baseVal = 0;
            var stationLF = WorkDirectionEnum.None;
            if (MNL.IsChecked.HasValue && MNL.IsChecked.Value)
            {
                baseVal = 0;
                stationLF = WorkDirectionEnum.Left;
            }

            if (MNY.IsChecked.HasValue && MNY.IsChecked.Value)
            {
                baseVal = 5000;
                stationLF = WorkDirectionEnum.Right;
            }


            var code = ProductIndexHelper.GetIndex(Cache.Model, baseVal, Cache.WorkDirection, stationLF, true);
            Code.Text = code.ToString();
        }
    }
}
